package cn.exrick.xboot.modules.assessment.controller;


import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.config.custom.CustomPageUtils;
import cn.exrick.xboot.modules.assessment.entity.HkActType;
import cn.exrick.xboot.modules.assessment.entity.HkIndexClassify;
import cn.exrick.xboot.modules.assessment.service.IHkIndexClassifyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author yck
 * @since 2022-08-30
 */
@RestController
@RequestMapping("/performance/hkIndexClassify")
public class HkIndexClassifyController {
    private final Logger logger = LoggerFactory.getLogger(HkIndexClassifyController.class);

    @Autowired
    public IHkIndexClassifyService iHkIndexClassifyService;

    /**
     * 分页查询数据
     *
     * @param page            分页信息
     * @param hkIndexClassify 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHkIndexClassifyPageList")
    public Result<Object> getHkIndexClassifyPageList(Page<HkIndexClassify> page, HkIndexClassify hkIndexClassify) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHkIndexClassifyService.selectPage(page, hkIndexClassify)));
    }

    /**
     * 保存和修改公用的
     *
     * @param hkIndexClassify 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkIndexClassifySave")
    public Result<Object> hkIndexClassifySave(HkIndexClassify hkIndexClassify) {
        QueryWrapper<HkIndexClassify> query = Wrappers.query();
        query.lambda().eq(StringUtils.isNotBlank(hkIndexClassify.getIndexClassifyName()),HkIndexClassify::getIndexClassifyName,hkIndexClassify.getIndexClassifyName());
        HkIndexClassify hkIndexClassifyByName = iHkIndexClassifyService.getOne(query);
        if (hkIndexClassifyByName != null){
            return ResultUtil.error("该类别已存在");
        }
        iHkIndexClassifyService.saveOrUpdate(hkIndexClassify);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hkIndexClassifyGetById")
    public Result<Object> hkIndexClassifyGetById(String id) {
        HkIndexClassify byId = iHkIndexClassifyService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkIndexClassifyDelete")
    public Result<Object> hontUserDelete(String id) {
        iHkIndexClassifyService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hkIndexClassifyBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHkIndexClassifyService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }


    /**
     * 新增指标分类
     *
     * @param indexClassifyName
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/saveHkIndexClassifyByName")
    public Result<Object> saveHkIndexClassify(String indexClassifyName) {
        return iHkIndexClassifyService.saveHkIndexClassify(indexClassifyName);
    }

    /**
     * 获取所有指标分类
     *
     * @param
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hkIndexClassifyGetAll")
    public Result<Object> hkIndexClassifyGetAll() {
        HkIndexClassify hkIndexClassify = new HkIndexClassify();
        List<HkIndexClassify> hkIndexClassifies = iHkIndexClassifyService.selectList(hkIndexClassify);
        return ResultUtil.data(hkIndexClassifies);
    }
}
